﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Newtonsoft.Json;
using System.IO;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;

namespace WpfBeyond.pages
{
    /// <summary>
    /// PageSetting.xaml 的交互逻辑
    /// </summary>
    public partial class PageSetting : Page
    {
        ffmpeg_mode_list p_ffmpeg_mode_list;

        MainWindow parentWindow;
        public MainWindow ParentWindow
        {
            get { return parentWindow; }
            set { parentWindow = value; }
        }

        public PageSetting()
        {
            InitializeComponent();

            p_ffmpeg_mode_list = new ffmpeg_mode_list(cmb_ffmpeg_mode);

            txt_server_port.Text = LocalSetting.Items.Server_port.ToString();
            txt_reback_port.Text = LocalSetting.Items.Reback_port.ToString();
            cmb_ffmpeg_mode.SelectedIndex = LocalSetting.Items.Ffmpeg_mode;
            txt_ffmpeg_rtp.Text = LocalSetting.Items.Ffmpeg_rtp;
            txt_ffmpeg_udp.Text = LocalSetting.Items.Ffmpeg_udp;
            num_savelog_days.Value = LocalSetting.Items.Savelog_days;
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                LocalSetting.Items.Server_port = int.Parse(txt_server_port.Text.Trim());
                LocalSetting.Items.Reback_port = int.Parse(txt_reback_port.Text.Trim());
                LocalSetting.Items.Ffmpeg_mode = cmb_ffmpeg_mode.SelectedIndex;
                LocalSetting.Items.Ffmpeg_rtp = txt_ffmpeg_rtp.Text;
                LocalSetting.Items.Ffmpeg_udp = txt_ffmpeg_udp.Text;
                LocalSetting.Items.Savelog_days = int.Parse(num_savelog_days.Value.ToString());

                //保存设置信息
                JsonSerializer serializer = new JsonSerializer();
                serializer.NullValueHandling = NullValueHandling.Ignore;

                using (StreamWriter sw = new StreamWriter(LocalSetting.setting_JsonFileName))
                using (JsonWriter writer = new JsonTextWriter(sw))
                {
                    serializer.Serialize(writer, LocalSetting.Items);
                }

                Common.ShowMessage("提示", "保存配置成功！");
            }
            catch (Exception ex)
            {
                Common.ShowMessage("提示", "保存配置是失败！错误：" + ex.Message);
            }
        }
                
    }
}
